﻿
using Dapper;
using hdb;
using hentity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace hwms {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/
    public  class InOutComponent<CS, CSD> :GkTransaction where CS : AbsCoreStock where CSD : AbsCoreStockDetail, new()
    {
        public int src = 0;
        public int des = 0;
        public string boxCode;
        void continueTask(IDbConnection conn, IDbTransaction trans, CoreTask ct)
        {
            String sql = $"select * from Core_task_param where task_id=@taskId  and type={(int)TaskParamType.托盘搬运} and status <= {(int) CoreTaskParamStatus.下发完成}";
            List<CoreTaskParam> list = conn.Query<CoreTaskParam>(sql, new { taskId = ct.id }, trans).ToList();
            if (list.Count > 0)
            {
                return;
            }
           CS cs= conn.GetById<CS>(ct.stockId ,trans);
           if ( cs.status == (int) StockStatus.盘库中)
            {
                ParamContent pc = new ParamContent {   };
                TaskPersistenceUtil.moveCoreTaskParam(conn, trans, pc, ct.id,getStn(), ct.src, 0);
            }
        }
         protected override TransRes Execute(IDbConnection conn , IDbTransaction tran)
        {
            List<CoreTask> coreTaskList = checkCodeInWork(conn ,tran);
            if (coreTaskList.Count == 1)
            {
                continueTask(conn ,tran , coreTaskList[0]);
                return new TransRes();
            }
            else if (coreTaskList.Count > 1)
            {
           
                return new TransRes(true,getBoxCode() + "at" + getStn() + "working 不能越库!");
             }
            before(conn ,tran);
            CS cs = GetStockList(conn ,tran);
            if (cs == null)
            {
                return new TransRes(getBoxCode() + "at" + getStn() + "is null 不能越库!");
            }
            if (cs.status != 0)
            {
                return new TransRes(getBoxCode() + "at" + getStn() + "状态不能越库!" + cs.status);
               
            }
            // 可能原来入库站台，与正式入库口不一直。
            cs.locId = getStn();
            long stockId = cs.id;
             if (stockId == 0)
            {
                new TransRes(stockId + "不存在");
            }
            List<CSD> stockDetailsList = CsdUtil.GetStockDetailList<CSD>(conn ,tran ,cs.id);
            long goodDiff = LocConfig.getGoodDiff(conn ,tran ,stockDetailsList[0] ,cs);
            TaskPersistenceUtil. createInOutTask(goodDiff,"" ,conn ,tran ,cs ,des  ,"" );
            TransRes res= afterIntask(conn ,tran , cs);
            return res;
        }
         protected virtual TransRes afterIntask(IDbConnection conn, IDbTransaction trans, CS cs)
        {
            return new TransRes();
        }
         protected virtual void before(IDbConnection connection, IDbTransaction transaction)
        {
        }
        
      
        protected virtual string getBoxCode()
        {
            return boxCode;
        }
          protected virtual int getStn()
        {
            return src;
        }
        protected virtual List<CoreTask> checkCodeInWork(IDbConnection connection, IDbTransaction transaction)
        {
            string sql = "select * from Core_Task where  status <9 and  status>0 ";
            String boxCode = getBoxCode();
            if (!String.IsNullOrWhiteSpace(boxCode))
            {
                sql += "  and box_Code=@boxCode  ";
            }
            else
            {
                sql += "  and stn =  " + getStn();
            }
             List<CoreTask> coreTaskList = connection.Query<CoreTask>(sql, new
            {
                boxCode
            }, transaction).ToList();
            return coreTaskList;
        }
           public List<CS> GetStock(IDbConnection Connection ,IDbTransaction transaction) {
            string sql = @"select * from Core_stock where  status in (0,1,2,3,4,5,6,7) ";
            String boxCode = getBoxCode();
            if( !String.IsNullOrWhiteSpace(boxCode) ) {
                sql += "  and box_Code=@boxCode  ";
            } else {
                sql += "  and loc_Id =  " + getStn();
            }
            List<CS> stockList = Connection.Query<CS>(sql ,new {
                boxCode
            } ,transaction).ToList();
            return stockList;
        }
         public CS GetStockList(IDbConnection Connection ,IDbTransaction transaction) {
            List<CS> stockList = GetStock(Connection ,transaction);
            if( stockList.Count == 0 ) {
                String info = getBoxCode() + "没有库位异常stock!";
                throw new Exception(info);
            }
            if( stockList.Count > 1 ) {
                String info = getBoxCode() + "重复的库位异常stock!";
                throw new Exception(info);
            }
            CS cs = stockList[0];
            return cs;
        }
    }
}
